//
//  LYBaseNavController.swift
//  微博
//
//  Created by liuerying on 16/5/20.
//  Copyright © 2016年 liuerying. All rights reserved.




import UIKit
//遵守协议,解决手势滑动
class LYBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置代理 添加手势滑动
        self.interactivePopGestureRecognizer?.delegate = self

    
    }
    
    //重写push方法 获取所有push 操作
    override func pushViewController(viewController: UIViewController, animated: Bool) {

       var backTitle = "返回"
        //判断如果角标大于0 ,
        if childViewControllers.count > 0{
        //并且等于1时,设置左边title为首页的title
            if childViewControllers.count == 1 {
            
                backTitle = childViewControllers.first?.title ?? "返回"
                
            }
            //设置图片及文字
                let back = UIBarButtonItem(imageName:"navigationbar_back_withtext", title: backTitle, target: self, action: #selector(LYBaseNavController.popToFontVC))
                
                viewController.navigationItem.leftBarButtonItem = back
                
   
        }

        super.pushViewController(viewController, animated: animated)
       //测试打印自控制器
//        print(childViewControllers.count)
    }
    
    @objc private func popToFontVC(){
        
      popViewControllerAnimated(true)
        
    }
    
    //实现手势协议的方法
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
//        if childViewControllers.count > 1 {
//        
//            return true
//        }
//        return false
        
        
        //自动判断比较,如果大于自控制器的大于1 就返回ture,如果是小于或者等于1,就位false
        return childViewControllers.count > 1
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
